
/*查看会员信息*/
function show(obj,id){
    $("#cid").attr("data-id",id);
    $("#head_imgUrl").attr("src","");
    $.ajax({
        url:getcarduserinfos,
        type:"post",
        data:{
            id:id
        },
        success:function(data){
            // console.log(data);
            $("#openid").attr("data-id",data.data.user.openid);
            $("#code").attr("data-id",data.data.user.code);
            $("#top_mid").attr("data-id",data.data.user.top_mid);
            if(data.data.wxinfo.head_url==""){
                $("#head_imgUrl").attr("src","");
            }else{
                $("#head_imgUrl").attr("src",data.data.wxinfo.head_url);
            }

            $("#Recommend").html(data.data.user.sharename);
            if(data.data.user.phone==null){
                data.data.user.phone="未知"
            }
            $("#head_memberMobile").html("手机号码："+data.data.user.phone);
            $("#head_nickName").html("会员昵称："+data.data.user.nickname);
            if(data.data.user.name==null){
                data.data.user.name="未知"
            }
            $("#head_memberName").html("会员姓名："+data.data.user.name);
            $("#member_integral").html(data.data.user.bonus);
            $("#member_zbalance").html(data.data.user.balance);
            $("#member_number").html(data.data.user.code);
            $("#member_phone").html(data.data.user.phone);
            $("#member_Coupon").html(data.data.cards);
            $("#member_shop").html(data.data.user.merchant);
            $("#member_email").html(data.data.user.email);
            $("#member_birth").html(data.data.user.birthday);
            if(data.data.user.zodiac=="null"){
                data.data.user.zodiac="未获取";
            }
            $("#member_constellation").html(data.data.user.zodiac);
            $("#member_sex").html(data.data.user.sex);
            $("#member_grade").html(data.data.user.level);
            //console.log(11,data.data.user.age);
            if(data.data.user.age=="null"){
                data.data.user.age="未获取";
            }
            $("#member_no").html(data.data.user.age);
            $("#member_address").html(data.data.user.address);
            $("#member_grade_name").html(data.data.user.level);
            $("#member_follow").html(data.data.user.is_follow);
            $("#member_register").html(data.data.user.create_time);




            $(".right-bar").addClass("open");
            $(".sidenav-overlay").addClass("overlay-active");
            $('#myTab a:first').tab('show')
        }
    })


}
// 会员信息
function getcarduserxfinfo(){
    var openid=$("#openid").attr("data-id");
    var code=$("#code").attr("data-id");
    var top_mid=$("#top_mid").attr("data-id");
    $.ajax({
        url:getcarduserxfinfos,
        type:"post",
        data:{
            openid:openid,
            code:code,
            top_mid:top_mid

        },
        success:function(data){
            console.log(999,data);
            if(data.average.del_balance!==0){
                data.average.del_balance = data.average.del_balance;
            }
            if(data.average.discount!==0){
                data.average.discount = data.average.discount;
            }

            if(data.average.add_balance!==0){
                data.average.add_balance = data.average.add_balance;
            }

            $("#wx_balance").html(data.xfcount.wx_xf_num);
            $("#del_balance").html(data.xfcount.del_balance);
            $("#xfadd_balance").html(data.xfcount.add_balance);
            $("#add_bonus").html(data.xfcount.add_bonus);
            $("#xf_nume").html(data.xfcount.xf_nume);
            $("#cz_nume").html(data.xfcount.cz_nume);
            $("#del_bonus").html(data.xfcount.del_bonus);
            $("#get_cards").html(data.xfcount.get_cards);
            $("#member_merchant").html(data.lately.merchant);
            $("#member_pay_balance").html(data.lately.pay_balance);
            $("#member_pay_time").html(data.lately.pay_time);
            $("#pjdel_balance").html(data.average.del_balance);
            $("#pjadd_balance").html(data.average.add_balance);
            $("#discount").html(data.average.discount);

        }
    })

}
function NumberCheck(t){
    var num = t.value;
    var re=/^\d*$/;
    if(!re.test(num)){
        isNaN(parseInt(num))?t.value=0:t.value=parseInt(num);
    }
}


$.ajax({
    type: 'POST',
    url: getbonusbizputs,
    data: {},
    success: function (data) {
        // console.log(2111,data);
        if(data.code==0){
            $("#form-self").attr("style","display:block");
            var th='<tr><td>'+ data.data.end_time+'</td>\n' +
                '<td>'+ data.data.card+'</td></tr>';
            $("#tr").html(th);
        }
    }
})

function fafirexx(obj){

    var add_bonuss_mass=$("#add_bonuss_mass").val();
    var remark_mass=$("#remark_mass").val();
    if(add_bonuss_mass==""||add_bonuss_mass==0){
        layer.alert("请输入赠送积分！");
        return false;
    }

    if(remark_mass==""){
        layer.alert(" 请输入消息内容！");
        return false;
    }

    $(".progress-striped").attr("style", "display:block");
    $("#progress-bar1").attr("style", "width:0");
    var xhrOnProgress = function (fun) {
        xhrOnProgress.onprogress = fun; //绑定监听
        //使用闭包实现监听绑
        return function () {
            //通过$.ajaxSettings.xhr();获得XMLHttpRequest对象
            var xhr = $.ajaxSettings.xhr();
            //判断监听函数是否为函数
            if (typeof xhrOnProgress.onprogress !== 'function')
                return xhr;
            //如果有监听函数并且xhr对象支持绑定时就把监听函数绑定上去
            if (xhrOnProgress.onprogress && xhr.upload) {
                xhr.upload.onprogress = xhrOnProgress.onprogress;
            }
            console.log(xhr);
            return xhr;
        }
    }
    $.ajax({
        type: 'POST',
        url: sendcardsxx,
        data: $("#tests1").serializeArray(),
        xhr: xhrOnProgress(function (e) {
            var percent = e.loaded / e.total;
            console.log(33,percent);
            $("#progress-bar1").attr("style", "width:"+(percent * 100)+"%");

        }),

        success: function (data) {

            if(data.code == 0){
                layer.alert(data.message);

                $('#myModal5').modal('hide');
                $(".progress-striped").attr("style","display:none;")
                $(obj).attr("disabled",false);
            }else{
                $(obj).attr("disabled",'disabled');
                layer.alert(data.message);
            }
        }
    })
}


function delsel(obj) {
//
    $(obj).parent().remove();


}
// ajax + jQuery上传
function fafirecard(obj) {

    var add_bonuss=$("#add_bonuss").val();
    var text=$("#text").val();
    var va = $("#opt").val();
    if(add_bonuss==""||add_bonuss==0){
        layer.alert("请输入赠送积分！");
        return false;
    }
    var items =  $("select[name='card_arr[zs_card_id][]']"),
        len = items.length,
        i = 0,
        valMap = {},
        sel,
        val;

    // 遍历并保存各个下拉列表的值于valMap
    for (; i < len; i++) {
        sel = items[i];
        val = sel.options[sel.selectedIndex].value;
        console.log(items[i]);
        // 没有相同的取值时保存该值，否则返回false
        if (!valMap[val]) {
            valMap[val] = 1;
        } else {
            layer.alert('请删除重复的卡券！');
            return false;
        }


    }
    if (va == "") {
        layer.alert(" 请选择卡券！");
        return false;
    }
    if(text==""){
        layer.alert(" 请输入消息内容！");
        return false;
    }

    $(".progress-striped").attr("style", "display:block");
    $("#progress-bar").attr("style", "width:0");
    var xhrOnProgress = function (fun) {
        xhrOnProgress.onprogress = fun; //绑定监听
        //使用闭包实现监听绑
        return function () {
            //通过$.ajaxSettings.xhr();获得XMLHttpRequest对象
            var xhr = $.ajaxSettings.xhr();
            //判断监听函数是否为函数
            if (typeof xhrOnProgress.onprogress !== 'function')
                return xhr;
            //如果有监听函数并且xhr对象支持绑定时就把监听函数绑定上去
            if (xhrOnProgress.onprogress && xhr.upload) {
                xhr.upload.onprogress = xhrOnProgress.onprogress;
            }
            console.log(xhr);
            return xhr;
        }
    }
    $.ajax({
        type: 'POST',
        url: sendbonusbizcards,
        data: $("#tests").serializeArray(),
        xhr: xhrOnProgress(function (e) {
            var percent = e.loaded / e.total;
            console.log(33,percent);
            $("#progress-bar").attr("style", "width:"+(percent * 100)+"%");

        }),

        success: function (data) {

            if(data.code == 0){
                layer.alert(data.message);

                $('#myModal5').modal('hide');
                $(".progress-striped").attr("style","display:none;")
                $(obj).attr("disabled",false);
            }else{
                $(obj).attr("disabled",'disabled');
                layer.alert(data.message);
            }
        }
    })
}


laydate.render({
    elem: '#times',
    type: 'date',
    range: true,
    change: function (value, date, date2, data3) {
        var s = value.split(" - ");
        var startdate = new Date(s[0]).getTime();
        var enddate = new Date(s[1]).getTime();
        var ldate = Math.floor((enddate - startdate) / (24 * 3600 * 1000));

    }
});



// 分页ajax
function pagelist(url,tbody,pagetext,successfn){
    var p=1;
    var page=1;
    var openid=$("#openid").attr("data-id");
    var code=$("#code").attr("data-id");
    var top_mid=$("#top_mid").attr("data-id");
    var tr="";

    $("#"+tbody).attr("data-text",p);
    var curPage = 1; //当前页码
    var total,pageSize,totalPage; //总记录数，每页显示数，总页数
    getData(1);
    $("#"+pagetext).on('click','span a',function(){
        var rel = $(this).attr("rel");
        if(rel){
            getData(rel);
        }
    });
    function getData(page) {
        $.ajax({
            url:url,
            type:"post",
            data:{
                openid:openid,
                code:code,
                top_mid:top_mid,
                page:page
            },
            success: function (data) {
                successfn(data)
                curPage = page; //当前页
                pageSize = 10; //每页显示条数
                if(data.code=='9001'){
                    total=0;
                    totalPage=0;
                }else{
                    total = parseInt(data.conut); //总记录数
                    totalPage = parseInt(data.pagenum); //总页数
                }



            },
            complete:function(data){ //生成分页条
                // console.log(99,data.responseJSON);
                getPageBar();

            },
            error:function(){
                alert("数据加载失败");
            }
        });
    }
    function getPageBar(){
        $("#"+pagetext).find('*').remove();
        //页码大于最大页数
        if(curPage>totalPage) curPage=totalPage;
        //页码小于1
        if(curPage<1) curPage=1;
        pageStr = "<span>共"+total+"条</span><span>"+curPage+"/"+totalPage+"</span>";

        //如果是第一页
        if(curPage==1){
            pageStr += "<span>首页</span><span>上一页</span>";
        }else{
            pageStr += "<span><a href='javascript:void(0)' rel='1'>首页</a></span><span><a href='javascript:void(0)' rel='"+(curPage-1)+"'>上一页</a></span>";
        }

        //如果是最后页
        if(curPage>=totalPage){
            pageStr += "<span>下一页</span><span>尾页</span>";
        }else{
            pageStr += "<span><a href='javascript:void(0)' rel='"+(parseInt(curPage)+1)+"'>下一页</a></span><span><a href='javascript:void(0)' rel='"+totalPage+"'>尾页</a></span>";
        }
        $("#"+pagetext).append(pageStr);
    }

}


/********备注********/


/*编辑会员信息*/
function editMember(){
    var id=$("#cid").attr("data-id");
    $.ajax({
        url:getcarduserinfos,
        type:"post",
        data:{
            id:id
        },
        success:function(data){
            //console.log(77,data);
            $("#e_mobile").val(data.data.user.phone);
            $("#e_email").val(data.data.user.email);
            $("#e_nickName").val(data.data.user.nickname);
            $("#e_memberName").val(data.data.user.name);
            $("#e_birthday").val(data.data.user.birthday);

            $("#e_point").val(data.data.user.bonus);
            $("#m_status").val(data.data.user.status);
            if(data.data.user.sex=='男'){

                $("#m_sex").val('1');
            }
            if(data.data.user.sex=='女'){
                //console.log(1123);
                $("#m_sex").val('0');
            }
            $("#levelId").val(data.data.user.level);
            $("#merchantId").val(data.data.user.merchant);
            $("#code1").val(data.data.user.code);
            $("#openid1").val(data.data.user.openid);

            $(".right-bar").addClass("open");
            $(".sidenav-overlay").addClass("overlay-active");
        }
    })

    $('#myModal3').modal('show')
}
$("#btn-primary").click(function(){
    $.ajax({
        type: 'post',
        url: getcarduseredits,
        data: $("#editMemberForm").serialize(),
        success: function(data) {
            // console.log(99,data);
            if(data.code=='0'){
                alert(data.message);

                $('#myModal3').modal('hide')
                location.reload();
            }
            if(data.code=='9001'){
                alert(data.message);
            }
            //console.log(data);
            // your code
        }
    });
})



$(".sidenav-overlay").click(function(){

    $(".right-bar").removeClass("open");
    $(".sidenav-overlay").removeClass("overlay-active");
    // location.reload();
})

var czhtml = $("#czshow").html();
function czkindfun(value){
    if(value=='1'){
        $("#czname").text("充值金额：");
        $("#czje").removeClass("hidden");
        $("#czshow").html(czhtml);
        $("#czadd_balance").attr("name","add_balance");
    }else if(value="2"){
        $("#czadd_balance").attr("name","add_bonus");
        $("#czshow").html('');
        $("#czname").text("充值积分：");
        $("#czje").removeClass("hidden");
    }
}
function changetype(value){
    if(value=='1'){
        $("#he-name").text("核销金额：");
        $("#add_balance").attr("name","add_balance");
        $("#record_balance").attr("name","record_balance");
    }else if(value="2"){
        $("#he-name").text("核销积分：");
        $("#add_balance").attr("name","bonus");
        $("#record_balance").attr("name","record_bonus");
    }
}
function getDatas(val){
    var url= requestnames;
    $.ajax({
        url:url,
        type:"post",
        data:{
            merchant:val
        },
        success:function(data){
            if(data!=""){
                var list = "";
                for(var i=0;i<data.length;i++){
                    list+="<li>"+data[i].merchant+"</li>";
                }
                $(".searchlist ul").html(list);
                $(".searchlist").removeClass("hide");
            }else{
                $(".searchlist ul").html('');
                $(".searchlist").addClass("hide");
            }
        }
    })
}
function hexiao(url,id,btn){
    $("#"+btn).attr("disabled","disabled");
    $.ajax({
        url:url,
        type:"post",
        data:$("#"+id).serialize(),
        success:function(data){
            // console.log(data)
            if(data.code==0){
                layer.msg(data.message,{icon:1,time:1000});
                $("#"+btn).removeAttr("disabled");
                if(id=="cz"){
                    $('#czModal').modal('hide')
                }else if(id=="hexiao"){
                    $('#hexiaoModal').modal('hide')
                }
                // setTimeout("window.location.reload()",1000)
                return false;
            }else{
                $("#"+btn).removeAttr("disabled");
                layer.alert(data.message,{icon:7});
                return false;
            }
        }
    })
}

//        获取充值说明
$("#cz").on('change','#czme',function(){
    // console.log('asdf');
    var val = $(this).val();
    var zs = $(this).find("option:selected").attr("data-zs");
    var zsnew = $(this).find("option:selected").attr("data-zsnew");
    var money = $(this).find("option:selected").attr("data-money");
    var html = "";
    if(val){
        $("#czsm-wrap").removeClass("hidden");
        $("#czje").addClass("hidden");
    }else{
        $("#czsm-wrap").addClass("hidden");
        $("#czje").removeClass("hidden");
    }
    if(money){
        money="赠送金额："+money+"<br/>"
    }
    if(zs){
        zs = "赠送卡券："+zs;
    }

    $("#czsm").html(money+zs+zsnew);
});
$("#czModal").on('show.bs.modal',function () {
    var id = $(event.target).attr("data-id");
    var name=$(event.target).attr("data-name");
    var code=$(event.target).attr("data-code");
    $("#czcard-name").html(name);
    $("#czcard-code").html(code);
    $("#czcard-id").val(id);
});
//        核销方法
$("#hexiaoModal").on('show.bs.modal',function () {
    var id = $(event.target).attr("data-id");
    var type = $(event.target).attr("data-type");
    var name=$(event.target).attr("data-name");
    var code=$(event.target).attr("data-code");
    // console.log(333,name);
    $("#card-id").val(id);
    $("#card-name").html(name);
    $("#card-code").html(code);
    $("#modal-type").val(type);
    if(type==1){
        $("#hexiaoModalLabel").text("会员卡充值");
    }else if(type==2){
        $("#hexiaoModalLabel").text("会员卡核销");
    }
});
$("#hexiaoModal").on('hide.bs.modal',function () {
    $("#card-id").val("");
    $("#he-md").val("");
    $("#record_bonus ").val("");
    $("#add_bonus ").val("");
});


// 会员生日
laydate.render({
    elem: '#e_birthday'
});
/********备注********/
function getremarklist() {

    var openid = $("#openid").attr("data-id");
    var code = $("#code").attr("data-id");
    var top_mid=$("#top_mid").attr("data-id");
    var url=getremarklists;

    var tbody='bzjl';
    var pagetext='pagecount4';
    pagelist(url,tbody,pagetext,function(data){
        var text = data.data;
        $("#"+tbody).html("");
        tr="";

        for (var i = 0; i < text.length; i++) {
            if(text[i].con_username==null){
                var con_username="";
            }else{
                con_username=text[i].con_username;
            }
            tr += '<tr><td><code>' + text[i].create_time + '</code></td><td>' + con_username+ ' </td><td>' + text[i].remark+ ' </td></tr>'

        }
        $('#'+tbody).append(tr);
    });

}
// 优惠券
function getcardusercardslist(){

    var openid = $("#openid").attr("data-id");
    var code = $("#code").attr("data-id");
    var top_mid=$("#top_mid").attr("data-id");
    var url=getcardusercardslists;
    var tbody='yhqlq';
    var pagetext='pagecount3';
    pagelist(url,tbody,pagetext,function(data){
        // console.log(1123,data);
        var text = data.data;
        $("#"+tbody).html("");
        tr="";

        for (var i = 0; i < text.length; i++) {
            //console.log(text[i].code);
            if(text[i].title==null){

                text[i].title="";
                //console.log(text[i].record_bonus);
            }
            tr += '<tr><td><code>' + text[i].create_time + '</code></td><td>' + text[i].card_type + ' </td><td>' + text[i].merchant+ ' </td><td>' + text[i].card_statu+ ' </td><td>' + text[i].title+ ' </td></tr>'

        }
        $('#'+tbody).append(tr);
    });



}
/*消费记录*/
function getcarduserxflist() {


    var openid = $("#openid").attr("data-id");
    var code = $("#code").attr("data-id");
    var url=getcarduserxflists;

    var tbody='xfjl';
    var pagetext='pagecount';
    pagelist(url,tbody,pagetext,function(data){
        var text = data.data;
        $("#"+tbody).html("");
        tr="";

        for (var i = 0; i < text.length; i++) {
            var balance=text[i].pay_balance-text[i].del_balance;
            balance = balance.toFixed(2);
            tr += '<tr><td><code>' + text[i].create_time + '</code></td><td>' + text[i].merchant + ' </td><td>' + text[i].num + '</td><td>会员卡支付 </td></tr>'

        }

        $("#"+tbody).append(tr);
    });
}
/*微信消费记录*/
function getcarduserwxxflist() {


    var openid = $("#openid").attr("data-id");
    var code = $("#code").attr("data-id");
    var url=getcarduserwxxflists;

    var tbody='wxxfjl';
    var pagetext='pagecounts1';
    pagelist(url,tbody,pagetext,function(data){
        var text = data.data;
        $("#"+tbody).html("");
        tr="";

        for (var i = 0; i < text.length; i++) {
            var balance=text[i].pay_balance-text[i].del_balance;
            balance = balance.toFixed(2);
            tr += '<tr><td><code>' + text[i].add_time + '</code></td><td>' + text[i].merchant + ' </td><td>' + text[i].price + '</td><td>' + text[i].pay_fs + ' </td><td>' + text[i].pay_cate + ' </td></tr>'

        }

        $("#"+tbody).append(tr);
    });
}
/*充值记录*/
function getcarduserczlist() {


    var openid = $("#openid").attr("data-id");
    var code = $("#code").attr("data-id");
    var url=getcarduserczlists;

    var tbody='cczje';
    var pagetext='pagecounts';
    pagelist(url,tbody,pagetext,function(data){
        var text = data.data;
        $("#"+tbody).html("");
        tr="";

        for (var i = 0; i < text.length; i++) {
            var balance=text[i].add_balance-text[i].pay_balance;

            tr += '<tr><td><code>' + text[i].create_time + '</code></td><td>' + text[i].merchant + ' </td><td>' +  text[i].num  + '</td><td>' + text[i].pay_fs + ' </td></tr>'

        }

        $("#"+tbody).append(tr);
    });
}
/*积分*/
function getcarduserbonuslist(){

    var openid=$("#openid").attr("data-id");
    var code=$("#code").attr("data-id");
    var top_mid=$("#top_mid").attr("data-id");
    var url=getcarduserbonuslists;

    var tbody='jfjl';
    var pagetext='pagecount2';
    pagelist(url,tbody,pagetext,function(data){
        var text = data.data;
        $("#"+tbody).html("");
        //console.log();
        tr="";

        for (var i = 0; i < text.length; i++) {
            // console.log(text[i].code);
            if(text[i].type==2){
                var f= "-"
                var num=  text[i].num
            }else{
                var f= "+"
                var num= text[i].num
            }

            if(text[i].record_bonus==null){

                text[i].record_bonus="";
                //console.log(text[i].record_bonus);
            }
            tr += '<tr><td><code>' + text[i].create_time + '</code></td><td>' +f+num + ' </td><td>' + text[i].cate+ ' </td></tr>'

        }

        $("#"+tbody).append(tr);

    });
}



